import { Component, Input, OnInit } from '@angular/core';
import { ModalController, AlertController, LoadingController } from "@ionic/angular";
import { AppGlobalService } from 'src/app/shared/service/app-global.service';
import { HttpService } from 'src/app/shared/service/http.service';
@Component({
  selector: 'app-open-member-invitation-modal',
  templateUrl: './open-member-invitation-modal.component.html',
  styleUrls: ['./open-member-invitation-modal.component.scss'],
})
export class OpenMemberInvitationModalComponent implements OnInit {

  @Input() tagData;
  constructor(
    public modalController: ModalController,
    public httpService: HttpService,
    public appGlobalService: AppGlobalService

  ) {

  }
  ngOnInit() {
    // 每次加载完自动提交submit
    if (this.tagData && this.tagData.data && this.tagData.data.head.length > 0) {
      const submit = this.tagData.data.submit;
      const params = {
        content: submit.content,
        // tslint:disable-next-line:no-string-literal
        id: submit.id,
        sessionId: localStorage.getItem('sessionId')
      };
      // 请求获取新数据
      this.httpService.post('application/submit', params, res => {
        // 请求数据成功
        if (res.success) {
          // this.modalController.dismiss({ dismissed: true });
        } else {
          // this.modalController.dismiss({ dismissed: true });
          // 请求数据失败
          this.appGlobalService.showToast(res.message, 2000, 'middle');
        }
      },
        err => {
          // 请求数据失败
          this.appGlobalService.showToast(err.message, 2000, 'middle');
        });
    }
  }


  gotoPage(obj) {
    try {
      if (obj) {
        this.modalController.dismiss({ dismissed: true });
        const targetObj = { pageId: obj.target, params: obj.params, sessionId: localStorage.getItem('sessionId') };
        this.appGlobalService.goToForward(targetObj);
      }
    } catch (error) {
    }
  }

  close() {
    this.modalController.dismiss({ dismissed: true });
    // 每次加载完自动提交submit
    if (this.tagData && this.tagData.data && this.tagData.data.head.length > 0) {
      const submit = this.tagData.data.submit;
      const params = {
        content: submit.content,
        // tslint:disable-next-line:no-string-literal
        id: submit.id,
        sessionId: localStorage.getItem('sessionId')
      };
      // 请求获取新数据
      this.httpService.post('application/submit', params, res => {
        // 请求数据成功
        if (res.success) {
          this.openMemberForInvitationModal(res);
          // this.modalController.dismiss({ dismissed: true });
        } else {
          // this.modalController.dismiss({ dismissed: true });
          // 请求数据失败
          this.appGlobalService.showToast(res.message, 2000, 'middle');
        }
      },
        err => {
          // 请求数据失败
          this.appGlobalService.showToast(err.message, 2000, 'middle');
        });
    }
  }


  async openMemberForInvitationModal(res) {
    try {
      if (res && res.data.head && res.data.head.length > 0) {
        const modal = await this.modalController.create({
          showBackdrop: true,
          cssClass: 'custom-popover',
          animated: false,
          component: OpenMemberInvitationModalComponent,
          componentProps: { tagData: res }
        });
        await modal.present();
        const modalData = await modal.onDidDismiss(); // 获取关闭传回的值
      }
    } catch (error) {
      console.log('error----->' + JSON.stringify(error));
    }
  }

}
